<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\NoteBundle\Model;
use Base\BaseModel;
use Model\NoteBundle\Setting\NoteSetting;

class NoteModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new NoteSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function commonAdd($params)
    {
        //设置默认值
        $defaults = array(
            "who"       => "0",
            "whom"      => "0",
            "what"      => "哇真是太赞了！！！！",
            "time"   => time()
        );
        $data = array();
        foreach ($defaults as $k => &$v) {
            if (isset($params[$k]) && $params[$k] !== "") {
                $data[$k] = $params[$k];
                $v = $params[$k];
            }
        }
        $qb = $this->getQueryBuilder();
        if (isset($params["id"])) {
            //编辑
            $qb->update("Note");
            $qb->where("id = :id")->setParameter("id",$params["id"]);
            foreach ($data as $key => $val) {
                $qb->set("$key",":$key")->setParameter(":$key",$val);
            }
        } else {
            $qb->insert("Note");
            $keys = array_keys($defaults);
            $values = array_values($defaults);
            $insertKeys = array();
            foreach ($keys as $k => $val) {
                $insertKeys[$val] = "?";
            }
            $qb->values($insertKeys)->setParameters($values);
        }
        $qb->execute();
        if ( $qb->getState() === 1) {
            return $params;
        } else {
            return $qb->getState();
        }
    }

    public function getListByLesson( $lessonId , $offset = 0 , $perPage = 10 )
    {
        $qb = $this->getQueryBuilder();
        $qb->from("Note");
        $qb->select("Note.id ,Note.what ,Note.time , User.account , User.nickName ,User.headPic ");
        $qb->leftJoin("Note","User","User","Note.who = User.id");
        $qb->where("whom = :whom")->setParameter("whom",$lessonId);
        $qb->orderBy("time","desc");
        $qb->setFirstResult($offset)->setMaxResults($perPage);
        return $qb->execute()->fetchAll();
    }

    public function countNote($lessonId)
    {
        $qb = $this->getQueryBuilder();
        $qb->from("Note");
        $qb->select("*");
        $qb->where("whom = :whom")->setParameter("whom",$lessonId);
        return $qb->execute()->rowCount();
    }
}